<?php
// title, subtitle and type of chart
function chartOptions($title, $subtitle, $type_chart, $width, $heigth) {
	
    $content= "var chart; 
				$(document).ready(function() {
					chart = new Highcharts.Chart({
						chart: {
							renderTo: 'container',
							defaultSeriesType: '$type_chart',
							 width: $width,
							 height: $heigth

						},
					title: { 
						text: '$title'
					},
					subtitle: { 
						text: '$subtitle'
					}, ";
	return $content;
}

//  quelques options de Pie chart...
function pieOptions($title, $type, $name, $width, $heigth) {
    return "var chart;
$(document).ready(function() {
   chart = new Highcharts.Chart({
      chart: {
         renderTo: 'container',
         plotBackgroundColor: null,
         plotBorderWidth: null,
         plotShadow: false,
		 width: $width,
		 height: $heigth
      },
      title: {
         text: '$title'
      },
      tooltip: {
         formatter: function() {
            return '<b>'+ this.point.name +'</b>: '+ this.y;
         }
      },
      plotOptions: {
         pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
               enabled: true
            },
            showInLegend: true
         }
      },
       series: [{
         type: '$type',
         name: '$name',
         data: [";
}

// built pie chart 
function buildPie($tab){
$content="";
	foreach ($tab as $line){
		settype($line[1], "float");
		$content.="['".$line[0]."' ,      ".$line[1]."],";
		
	}
	$content.="]}]";
	$content=str_replace(",]}]", "]}]", $content);
	$content.="});                });";				
return $content;	
}

// c'est pour remplir les donnees dans lea categories ( axe de x) ( ceux ne sont pas des dates ).
function tab_to_accolades_with_cotes($tab) {
    $content = "";
    for ($i = 1; $i < sizeof($tab); $i++) {
        $content.="'". $tab[$i]. "',";
    }
    $content.="],
	 labels: {
            rotation: -45,
            align: 'right'
         }";
    $content = str_replace(",]", "]", $content);
    return $content; //['1','1','2','3']
}

// c'est pour remplir les donnees dans lea categories ( axe de x) ( ceux sont des dates ).
function tab_to_accolades_with_cotes_for_date($tab) {
    $content = "";
    for ($i = 1; $i < sizeof($tab); $i++) {
        $content.="'" . $tab[$i]->format('Y-m-d') . "',";
    }
    $content.="],
	 labels: {
            rotation: -45,
            align: 'right'
         }";
    $content = str_replace(",]", "]", $content);
    return $content; //['1','1','2','3']
}

// c'est pour remplir les donnees dans les datas ( les courbes ).
function tab_to_accolades_without_cotes($tab) { // remplir les donnees dans le data
    $content = "";
    for ($i = 1; $i < sizeof($tab); $i++) {
        $content.="" . $tab[$i] . ",";
    }
    $content.="]},{";
    $content = str_replace(",]", "]", $content);
    return $content . "";
}

// return le nom de la courbe.
function title($title) {
    return "name: '" . $title . "', ";
}

// les options de yAxis
function echoOptions($coord_name, $type_chart) {
    $content="},
			yAxis: {
				title: {
					text: '" . $coord_name . "'
				}
			},
			tooltip: {
				 formatter: function() { 
					return ''+ this.x +': '+ this.y ";
			$content.="	 }	
			},
			legend: {
				enabled: false
			},
		 plotOptions: { 
					column: { 
					pointPadding: 0.2, 
					borderWidth: 0 
				 } 
			 },	";
    return $content;
}

// on la donne une matrice et ca nous sort la graph.
function buildChart_($matrice, $coord_name, $tab) {
    //$tab c'est un tableau qui contient le title, sub-title and type of chart.
	$content=chartOptions($tab[0],$tab[1],$tab[2],$tab[3],$tab[4]);
    $content.= "xAxis: {         
					categories: [";
    $content.=tab_to_accolades_with_cotes($matrice[0]);
    $content.=echoOptions($coord_name, $tab[2]);
    $content.="	 series: [{";
    for ($i = 1; $i < sizeof($matrice); $i++) {
        $content.=title($matrice[$i][0]);
        $content.="data: [";
        $content.=tab_to_accolades_without_cotes($matrice[$i]);
    }

    $content.="}); });";
    $content = str_replace(",{}", "]}", $content);
    return $content;
}

/*SI on a plusieurs colonnes...*/
function chartOptionsTwoColumns($render_to, $type, $title, $subtitle){
	$content="var chart;
			$(document).ready(function() {
			 var wid = screen.width/3.3;
			 var hei = screen.height/3.7;
				chart = new Highcharts.Chart({
					chart: {
						renderTo: '$render_to',
						defaultSeriesType: '$type',
						width: wid,
						height: hei
					},
					title: {
						text: '$title'
					},
				subtitle: {
					text: '$subtitle'
				},
				xAxis: {
					categories: [";
	return $content;
}
function chartOptionsTwoColumnsZoom($render_to, $type, $title, $subtitle){
	$content="var chart;
			$(document).ready(function() {
			 var wid = screen.width/3.3;
			 var hei = screen.height/3.7;
				chart = new Highcharts.Chart({
					chart: {
						renderTo: '$render_to',
						defaultSeriesType: '$type',
						width: 1000,
						height: 600
					},
					title: {
						text: '$title'
					},
				subtitle: {
					text: '$subtitle'
				},
				xAxis: {
					categories: [";
	return $content;
}

/* les options pour construire 2 colonnes */
function echoOptionsTwoColumns($coord_name){
	$content=" },
      yAxis: {
         min: 0,
         title: {
            text: '$coord_name'
         }
      },
	legend: {
		align: 'right',
		x: -100,
		verticalAlign: 'top',
		y: 2,
		floating: true
	},
      tooltip: {
         formatter: function() {
            return ''+
               this.x +': '+ this.y;
         }
      },
      plotOptions: {
         column: {
            pointPadding: 0.2,
            borderWidth: 0
         }
      },";
	 return $content;

}

/*Construire la fonction javascript...*/
function buildChartTwoColumns($matrice, $coord_name, $tab){
	$matrice[0][0]=$tab[2];
	$content=chartOptionsTwoColumns($tab[0], $tab[1],$matrice[0][0], $tab[3]);
	$content.=tab_to_accolades_with_cotes($matrice[0]); /* matrice [0] contient les noms AXE de X  et matrice[0][0]='title'*/
	$content.=echoOptionsTwoColumns($coord_name);
    $content.="	 series: [{";
	for ($i = 1; $i < sizeof($matrice); $i++) {
        $content.=title($matrice[$i][0]);
        $content.="data: [";
        $content.=tab_to_accolades_without_cotes($matrice[$i]);
    }
    $content.="}); });";
    $content = str_replace(",{}", "]}", $content);
    return $content;	
}

function buildChartTwoColumnsZoom($matrice, $coord_name, $tab){
	$matrice[0][0]=$tab[2];
	$content=chartOptionsTwoColumnsZoom($tab[0], $tab[1],$matrice[0][0], $tab[3]);
	$content.=tab_to_accolades_with_cotes($matrice[0]); /* matrice [0] contient les noms AXE de X  et matrice[0][0]='title'*/
	$content.=echoOptionsTwoColumns($coord_name);
    $content.="	 series: [{";
	for ($i = 1; $i < sizeof($matrice); $i++) {
        $content.=title($matrice[$i][0]);
        $content.="data: [";
        $content.=tab_to_accolades_without_cotes($matrice[$i]);
    }
    $content.="}); });";
    $content = str_replace(",{}", "]}", $content);
    return $content;	
}

?>